package cn.edu.ctbu.finaltest.service.TeacherService.impl;

import cn.edu.ctbu.finaltest.domain.Teacher;
import cn.edu.ctbu.finaltest.exception.REnum;
import cn.edu.ctbu.finaltest.repository.TeacherRepository;
import cn.edu.ctbu.finaltest.service.TeacherService.TeacherService;
import cn.edu.ctbu.finaltest.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherImplService implements TeacherService {

    @Autowired
    private TeacherRepository teacherRepository;


    /**
     * 查询所有教师
     * @return 返回List
     */
    public List<Teacher> findAll(){
        return teacherRepository.findAll();
    }
    public R<List<Teacher>> findRAll(){
        List<Teacher> teachers = teacherRepository.findAll();
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachers);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }


    public R<List<Teacher>> findByRName(String name){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (name.equals(teachers.get(i).getTname())){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }
    public R<List<Teacher>> findByRNameLike(String name){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (teachers.get(i).getTname().indexOf(name) >= 0){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }

    /**
     * 工号和密码寻找教师信息
     * @param tno
     * @param password
     * @return
     */
    public Teacher findByTnoAndPassword(String tno,String password){
        List<Teacher> teachers = teacherRepository.findAll();
        Teacher teacher = new Teacher();
        for (int i = 0;i < teachers.size();i++){
            if (tno.equals(teachers.get(i).getTno()) && password.equals(teachers.get(i).getPassword())){
                teacher = teachers.get(i);
                break;
            }
        }
        return teacher;
    }

    /**
     * 以学号查询教师
     * @param Tno
     * @return 返回Teacher
     */
    public Teacher findByTno(String Tno){
        List<Teacher> teachers = teacherRepository.findAll();
        Teacher teacher = new Teacher();
        for (int i = 0;i < teachers.size();i++){
            if (Tno.equals(teachers.get(i).getTno())){
                teacher = teachers.get(i);
            }
        }
        return teacher;
    }
    public R<Teacher> findByRTno(String Tno){
        List<Teacher> teachers = teacherRepository.findAll();
        Teacher teacher = new Teacher();
        for (int i = 0;i < teachers.size();i++){
            if (Tno.equals(teachers.get(i).getTno())){
                teacher = teachers.get(i);
            }
        }
        R<Teacher> RAll = new R<>();
        RAll.setData(teacher);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }
    public R<List<Teacher>> findByRTnoLike(String Tno){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (teachers.get(i).getTno().indexOf(Tno) >= 0){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }

    public R<List<Teacher>> findByRAge(Integer age){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (age == teachers.get(i).getAge()){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }
    public R<List<Teacher>> findByRAgeLike(Integer age){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (String.valueOf(teachers.get(i).getAge()).indexOf(String.valueOf(age)) >= 0){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }

    public R<List<Teacher>> findByRSex(String sex){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (sex.equals(teachers.get(i).getSex())){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }
    public R<List<Teacher>> findByRSexLike(String sex){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (teachers.get(i).getSex().indexOf(sex) >= 0){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }



    public R<List<Teacher>> findByRDept(String dept){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (dept.equals(teachers.get(i).getDept())){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }

    public R<List<Teacher>> findByRDeptLike(String dept){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (teachers.get(i).getDept().indexOf(dept) >= 0){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }

    /**
     * 以职称查询教师
     * @param prof
     * @return
     */
    public List<Teacher> findByProf(String prof){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (prof.equals(teachers.get(i).getProf())){
                teachersNew.add(teachers.get(i));
            }
        }
        return teachersNew;
    }

    public R<List<Teacher>> findByRProf(String prof){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (prof.equals(teachers.get(i).getProf())){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }

    public R<List<Teacher>> findByRProfLike(String prof){
        List<Teacher> teachers = teacherRepository.findAll();
        List<Teacher> teachersNew = new ArrayList<>();
        for (int i = 0;i < teachers.size();i++){
            if (teachers.get(i).getProf().indexOf(prof) >= 0){
                teachersNew.add(teachers.get(i));
            }
        }
        R<List<Teacher>> RAll = new R<>();
        RAll.setData(teachersNew);
        RAll.setMsg(REnum.SUCCESS.getMsg());
        RAll.setCode(REnum.SUCCESS.getCode());
        return RAll;
    }

    /**
     * 教师信息的增加
     * @param teacher
     * @return 返回Teacher
     */
    public Teacher insert(Teacher teacher){
        teacherRepository.save(teacher);
        return teacher;
    }

    /**
     * 教师信息的更新
     * @param teacher
     * @return
     */
    public Teacher update(Teacher teacher){
        teacherRepository.save(teacher);
        return teacher;
    }

    /**
     * 教师信息通过id删除
     * @param id
     */
    public void deleteId(Integer id){
        teacherRepository.deleteById(id);
    }

}
